<?php

namespace app\models;

use app\components\Func;
use Yii;

/**
 * This is the model class for table "wx_account".
 *
 * @property int $id
 * @property string $title
 * @property string $raw_id
 * @property string $app_id
 * @property string $app_secret
 * @property string $msg_token
 * @property int $account_pay_id
 */
class WxAccountMini extends WxAccount
{
    //获取tokenＵＲＬ
    const URL_GET_TOKEN = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
    const URL_MINI_QR_A = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=%s';
    const URL_MINI_QR_B = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s';
    const URL_MINI_QR_C = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s';

    static public function getObjByAppid($appid){
        return self::findOne(['app_id'=>$appid]);
    }
    //获取用户小程序帐号
    static public function getUserAccount(){
        return self::findOne(1);
    }
    //获取用户小程序帐号
    static public function getSchoolAccount(){
        return self::findOne(2);
    }
    //统一检查微信接口返回数据是否错误
    static public function checkError($wx_result){
        $arr = json_decode($wx_result, 1);
        if(isset($arr['errcode']) && $arr['errcode'] > 0){
            throw new \Exception('微信接口:'.$arr['errmsg'], $arr['errcode']);
        }
        return $arr;
    }
    //获取小程序B类二维码
    public function getMiniQrB($arr = []){
        $url = sprintf(self::URL_MINI_QR_B, $this->getToken());
        $result = Func::postUploadData($url,json_encode($arr,JSON_UNESCAPED_UNICODE));
        self::checkError($result);
        return $result;
    }
    //获取token
    public function getToken($flush = 0){
        $key = 'wx:token:'.$this->id;
        //强制刷新token
        if($flush)
        {
            $url = sprintf(self::URL_GET_TOKEN, $this->app_id, $this->app_secret);
            $arr = self::checkError(Func::file_get_contents($url));
            Yii::$app->redis->setex($key,$arr['expires_in'],$arr['access_token']);
            return $arr['access_token'];
        }
        $token = Yii::$app->redis->get($key);
        if(empty($token)) return call_user_func_array(array($this, __METHOD__), array(1));
        return $token;
    }
    //关联应用
    public function getAccountPay(){
//        return $this->hasOne(WxAccountPay::className(),['id'=>'account_pay_id']);
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'wx_account';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'app_id', 'app_secret'], 'required'],
            [['account_pay_id'], 'integer'],
            [['title', 'raw_id', 'app_id', 'app_secret', 'msg_token'], 'string', 'max' => 255],
            [['app_id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'raw_id' => 'Raw ID',
            'app_id' => 'App ID',
            'app_secret' => 'App Secret',
            'msg_token' => 'Msg Token',
            'account_pay_id' => 'Account Pay ID',
        ];
    }
}
